// pages/message-detail/message-detail.js
Page({
  data: {
    messageInfo: null,
    replyContent: '',
    isReceiver: false,
    baseUrl: 'http://localhost:3000'
  },

  onLoad: function(options) {
    if (options.id) {
      this.loadMessageDetail(options.id);
    }
  },

  loadMessageDetail: function(messageId) {
    const { baseUrl } = this.data;
    const userInfo = wx.getStorageSync('userInfo');
    
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: `${baseUrl}/api/messages/${messageId}`,
      method: 'GET',
      header: {
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      },
      success: (res) => {
        if (res.data.success) {
          const messageInfo = res.data.message;
          const isReceiver = messageInfo.receiverId === userInfo.id;
          
          this.setData({
            messageInfo,
            isReceiver
          });
        } else {
          wx.showToast({
            title: '获取消息详情失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('获取消息详情失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  handleReplyInput: function(e) {
    this.setData({
      replyContent: e.detail.value
    });
  },

  handleApprove: function() {
    const { messageInfo, replyContent, baseUrl } = this.data;
    
    wx.showLoading({
      title: '处理中...'
    });

    wx.request({
      url: `${baseUrl}/api/messages/${messageInfo.id}/approve`,
      method: 'POST',
      header: {
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      },
      data: {
        reply: replyContent
      },
      success: (res) => {
        if (res.data.success) {
          wx.showToast({
            title: '已同意请求',
            icon: 'success'
          });
          this.loadMessageDetail(messageInfo.id);
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('处理消息失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },

  handleReject: function() {
    const { messageInfo, replyContent, baseUrl } = this.data;
    
    if (!replyContent.trim()) {
      wx.showToast({
        title: '请输入拒绝原因',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '处理中...'
    });

    wx.request({
      url: `${baseUrl}/api/messages/${messageInfo.id}/reject`,
      method: 'POST',
      header: {
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      },
      data: {
        reply: replyContent
      },
      success: (res) => {
        if (res.data.success) {
          wx.showToast({
            title: '已拒绝请求',
            icon: 'success'
          });
          this.loadMessageDetail(messageInfo.id);
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('处理消息失败:', err);
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  }
});